#include "mainwindow.h"
#include "ui_mainwindow.h"



extern int N,M,w,w1,w2,num,wi,a,b,c;//N,M si string的下标 num 订单总数 wi表示finishtime的下表
extern int flag; //flag 表示现在是否处于爆单状态
extern long long finishtime[55000];//待完成订单的完成时间
extern char strin[10000];//用来临时存储读到的字符串
extern std::string sss;
extern QTime baset;

FILE * fp1 = fopen("C:\\Users\\FXY\\Desktop\\file.txt","wb");

// 食物种类的结构体数组  名字 t maxcap currentcap
extern struct menun
{
    int t,maxcap,currentcap,leavetime;
    char name[150];
}nshiwu[250];

extern struct menum
{
    char name[150];
    int sum;
    int kinds[150];
}mtaocan[250];

extern QString sstr;

//定义开始时间
long long lasttime = 7*60*60;
long long numadd = 1;

bool checked;

//函数声明
//void timeinput();
void cpfr(int time);
long long njudge(int ni);
long long mjudge(int mi);
void wjudge(long long t);
//void timeprintf(long long t);

//D:\Qt\online\qt6

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->timebutton->hide();
    ui->timeeditor->hide();
    ui->background->setStyleSheet("background:pink");
    //logo
    QIcon con4("D:\\Qt\\online\\qt6\\4.png");
    QIcon con3("D:\\Qt\\online\\qt6\\3.png");
    ui->logo->setIcon(con4);

    //设置时间触发
    ptimer = new QTimer;
    //链接动态按钮的槽函数
    connect(ptimer,&QTimer::timeout,this,&MainWindow::timeoutslot);
}

MainWindow::~MainWindow()
{
    delete ui;
}


//fsj
//处理订单信息，并返回完成时间
void MainWindow::timeinput()
{
    //int a,b,c;//时，分，秒



    long long betweentime,currenttime;


    currenttime = a*60*60+b*60+c;
    betweentime = a*60*60+b*60+c-lasttime;

    if (currenttime<7*60*60 || currenttime>22*60*60)
    {
        timeprintf(-1);
    }



    wjudge(currenttime);

    if (flag)
    {
        timeprintf(-1);
    }

    //计算时间和储量
    //cpfr(betweentime);

    //匹配食物 写成函数
    for (int i = 0; i < N; i++)
    {
        if (strcmp(strin,nshiwu[i].name) == 0)
        {
            //qDebug()<<i;
            long long tjudge = njudge(i);

            if (tjudge == 0)//可以立即出餐
            {
                nshiwu[i].currentcap--;
                timeprintf(tjudge+currenttime);//时间打印函数
                lasttime = currenttime;//时间差修改
                break;
            }
            else
            {
                w++;
                if (w > w1)
                {
                    flag = 1;
                }
                timeprintf(tjudge+currenttime);
                nshiwu[i].currentcap--;
                lasttime = currenttime;//时间差修改
                //完成时间数组也要修改
                finishtime[wi++] = tjudge+currenttime;

            }
        }

    }

    //匹配套餐
    for (int i = 0; i < M; i++)
    {
        if (strcmp(strin,mtaocan[i].name) == 0)
        {
            long long tjudge = mjudge(i);
            if (tjudge == 0)//可以立即出餐
            {
                for (int j = 0; j < mtaocan[i].sum; j++)
                {
                    nshiwu[mtaocan[i].kinds[j]].currentcap--;
                }

                timeprintf(tjudge+currenttime);//时间打印函数
                lasttime = currenttime;//时间差修改
                break;
            }
            else
            {
                w++;
                if (w > w1)
                {
                    flag = 1;
                }

                timeprintf(tjudge+currenttime);
                //套餐中食物容量减
                for (int j = 0; j < mtaocan[i].sum; j++)
                {
                    //printf("%d ",nshiwu[mtaocan[i].kinds[j]].currentcap);
                    nshiwu[mtaocan[i].kinds[j]].currentcap--;
                    //printf("%d ",mtaocan[i].kinds[j]);
                }

                lasttime = currenttime;//时间差修改
                //完成时间数组也要修改
                finishtime[wi++] = tjudge+currenttime;

            }
        }
    }

    //printf("未能找到食物或套餐\n");

}

//更新食物
void cpfr(int time)
{

    for (int i = 0; i < N; i++)
    {

        nshiwu[i].currentcap = nshiwu[i].currentcap+(time+nshiwu[i].leavetime)/nshiwu[i].t;

        if (nshiwu[i].currentcap>=nshiwu[i].maxcap)
        {
            nshiwu[i].currentcap = nshiwu[i].maxcap;
            nshiwu[i].leavetime = 0;
        }
        else
        {
            nshiwu[i].leavetime = (time+nshiwu[i].leavetime)%nshiwu[i].t;
        }
    }

}

//返回制作所需的时间
long long njudge(int ni)
{
    if (nshiwu[ni].currentcap>0)
    {
        return 0;
    }
    else
    {
        return nshiwu[ni].t*(1-nshiwu[ni].currentcap)-nshiwu[ni].leavetime;
    }
}
long long mjudge(int mi)
{
    int sum = mtaocan[mi].sum;
    long long ti,t = 0;

    for (int i = 0; i < sum; i++)
    {
        ti = njudge(mtaocan[mi].kinds[i]);
        //printf("%d ",ti);
        if (t < ti)
        {
            t = ti;
        }
    }

    return t;
}

//判断是否处于爆单状态
void wjudge(long long t)
{
    w = 0;
    int ac = 0;
    //flag = 1;
    for (int i = 0; i < wi; i++)
    {
        if (t<finishtime[i])
        {
            w++;
        }
        if (t == finishtime[i])
        {
            ac++;
        }

    }


    if (flag == 1 && w < w2)
    {
        flag = 0;
        if (ac+w >= w2)
        {
            flag = 1;
        }
    }


    if (w > w1)
    {
        flag = 1;
    }

}

//返回完成时间和状态
void MainWindow::timeprintf(long long t)
{
    if (t == -1)
    {
        sprintf(strin,"系统繁忙，请您稍后尝试");
    }
    else
    {
        sprintf(strin,"%02lld:%02lld:%02lld\n", t / 3600, (t % 3600) / 60, (t % 3600) % 60);
        //添加历史订单
        addhistoryorder();
    }


}







//yyh
//开始点餐 启动
void MainWindow::on_start_clicked()
{
    //时间触发开始
    ptimer->start(TIMEOUT);
    //隐藏开始的按钮和点餐界面
    //ui->listViewstart->hide();
    ui->start->hide();

    //显示食物按钮和状态
    ui->shiwubutton->click();
    ui->shiwustation->click();
}

//退出点餐
void MainWindow::on_over_clicked()
{
    close();
}

//显示食物按钮
void MainWindow::on_shiwubutton_clicked()
{
    QString name;

    //避免每次点击重复添加组件，提前清空滚动栏
    QLayout* layout = ui->scrollAreaWidgetContents_2->layout();
    if (layout) {
        QLayoutItem* item;
        while ((item = layout->takeAt(0)) != nullptr) {
            delete item->widget();
            delete item;
        }
    }

    //添加单品和套餐按钮
    for (int i = 0; i < N; i++)
    {
        name = nshiwu[i].name;

        QPushButton*bt = new QPushButton(ui->scrollAreaWidgetContents_2);
        bt->setText(name);
        connect(bt,&QPushButton::clicked,this,&MainWindow::onButtonCliked);

        bt->setStyleSheet("background:yellow");
        ui->verticalLayout_2->addWidget(bt);
    }
    for (int i = 0; i < M; i++)
    {
        name = mtaocan[i].name;

        QPushButton*bt = new QPushButton(ui->scrollAreaWidgetContents_2);
        bt->setText(name);
        connect(bt,&QPushButton::clicked,this,&MainWindow::onButtonCliked);

        bt->setStyleSheet("background:yellow");
        ui->verticalLayout_2->addWidget(bt);
    }



}

//显示食物状态
void MainWindow::on_shiwustation_clicked()
{

    QString name;
    int currentcap;
    int leavetime;
    //避免每次点击重复添加组件，提前清空滚动栏
    QLayout* layout = ui->scrollAreaWidgetContents->layout();
    if (layout) {
        QLayoutItem* item;
        while ((item = layout->takeAt(0)) != nullptr) {
            delete item->widget();
            delete item;
        }
    }
    //添加食物状态
    for (int i = 0; i < N; i++)
    {
        name = nshiwu[i].name;
        currentcap = nshiwu[i].currentcap;
        leavetime = nshiwu[i].t-nshiwu[i].leavetime;
        QString content=QString("%1").arg(currentcap);
        QString time = QString("%1").arg(leavetime);
        QString color=QString("<font color=#999999>%1</font>的当前储量为<font color=#333333>%2</font>离做好下一个食物还有<font color=#333333>%3</font>秒").arg(name).arg(content).arg(time);
        QLabel*lab = new QLabel(ui->scrollAreaWidgetContents);
        lab->setText(color);
        ui->verticalLayout->addWidget(lab);
    }

}

//时间修改
void MainWindow::on_timeaccelerate_clicked()
{
    ui->timeeditor->show();
    ui->timebutton->show();
}

//时间修改确认
void MainWindow::on_timebutton_clicked()
{
    QString acc = ui->timeeditor->text();
    timeout = acc.toULong()*1000;
    ui->timeeditor->hide();
    ui->timebutton->hide();
}








//lxc
//计时器信号激发的槽函数
void MainWindow::timeoutslot()
{

    //增加TIMEOUT毫秒
    baset = baset.addMSecs(timeout);
    QString showtime  = baset.toString("hh:mm:ss");

    //更新食物状态
    cpfr(timeout/1000);
    ui->shiwustation->click();
    //showtime.setNum(12);
    ui->systermtime->setStyleSheet("color: yellow; font-size: 30px; font-family: Arial;");
    ui->systermtime->setText(showtime);

}

//动态按钮的槽函数
void MainWindow::onButtonCliked()
{
    //sender()获取按钮的指针
    QObject *obj = sender();
    QPushButton *btn = qobject_cast<QPushButton*>(obj);
    //获取按钮显示文本
    QString text = btn->text();

    ui->dingdan->setText(text);

}

//确定点餐
void MainWindow::on_querenxiadan_clicked()
{

    memset(strin,0,sizeof(strin));
    //读入时间
    sstr = baset.toString();
    sss = sstr.toStdString();
    int i = 0;
    while (1)
    {
        if (sss[i] == '\0')
        {
            break;
        }
        strin[i] = sss[i];
        i++;
    }
    sscanf(strin,"%d:%d:%d",&a,&b,&c);

    memset(strin,0,sizeof(strin));

    //读入食物
    sstr = ui->dingdan->text();



    sss = sstr.toStdString();
    //strcpy(sss,strin);
    i = 0;
    while (1)
    {
        if (sss[i] == '\0')
        {
            break;
        }
        strin[i] = sss[i];
        i++;
    }

    ui->dingdan->clear();

    timeinput();
    //显示结果
    ui->chucantime->setText(strin);
    ui->dingdan->setText("等待下单中");
}

//取消点餐
void MainWindow::on_canceldingdan_clicked()
{
    ui->dingdan->clear();
    ui->dingdan->setText("等待下单");
}

//添加历史订单
void MainWindow::addhistoryorder()
{
    QString time = strin;
    QString order = QString("%1").arg(numadd++);
    QString color=QString("第<font color=#999999>%1</font>个订单预计在<font color=#333333>%2</font>做好，请您及时取餐").arg(order).arg(time);

    //fwrite();
    memset(strin,0,sizeof(strin));
    //读入时间
    //sstr = baset.toString();
    sss = time.toStdString();
    int i = 0;
    while (1)
    {
        if (sss[i] == '\0')
        {
            break;
        }
        strin[i] = sss[i];
        //fwrite(strin,sizeof(char),1,fp1);
        i++;
    }
    //sscanf(strin,"%d:%d:%d",&a,&b,&c);
    fprintf(fp1,"%s\n",strin);


    QLabel*lab = new QLabel(ui->scrollAreaWidgetContents_3);
    lab->setText(color);
    ui->verticalLayout_6->addWidget(lab);
}

//暂停/启动 按钮
void MainWindow::on_puasebutton_clicked()
{
    if (checked == 0)
    {
        ptimer->stop();
        checked = 1;
    }
    else if (checked == 1)
    {
        ptimer->start();
        checked = 0;
    }
}




